//
//  HMApplyMoneyTableViewCell.m
//  HMMarket
//
//  Created by haohao on 2017/6/26.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMApplyMoneyTableViewCell.h"

#define rowHeight 49

@interface HMApplyMoneyTableViewCell ()<UITextFieldDelegate>



@property (weak, nonatomic) IBOutlet UILabel *nameLabel;


@end

@implementation HMApplyMoneyTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"*退货金额"];
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
    self.nameLabel.attributedText = AttributedStr;
}


- (void)setTotalMoney:(NSString *)totalMoney {
    _totalMoney = totalMoney;
    
    NSString *monryString = @"0";
    CGRect nameRect = [self returnRectWithString:monryString font:15.0];
    self.moneyLabel.text = monryString;
    self.moneyLabel.frame = CGRectMake(CGRectGetMaxX(_nameLabel.frame), 0, nameRect.size.width + 1, rowHeight);
    
    
    NSString *totalString = [NSString stringWithFormat:@"(最多¥%@)", totalMoney];
    CGRect totalrect = [self returnRectWithString:totalString font:12.0];
    self.totalLabel.text = totalString;

    self.totalLabel.frame = CGRectMake(KWidthSCREEN - totalrect.size.width - 10, 0, totalrect.size.width + 1, rowHeight);
}

- (void)setReturnMoney:(NSString *)returnMoney {
    _returnMoney = returnMoney;
    if ([_returnMoney floatValue] == 0) {
        _returnMoney = @"";
    }
    NSString *monryString = [NSString stringWithFormat:@"%@", _returnMoney];
//    CGRect nameRect = [self returnRectWithString:monryString font:15.0];
    self.moneyLabel.text = monryString;
    self.moneyLabel.frame = CGRectMake(CGRectGetMaxX(self.symbolLabel.frame), 0, 200, rowHeight);
    
//    CGRect totalFrame = self.totalLabel.frame;
//    self.totalLabel.frame = CGRectMake(CGRectGetMaxX(_moneyLabel.frame) + 5, 0, totalFrame.size.width, totalFrame.size.height);
}

- (CGRect)returnRectWithString:(NSString *)string font:(float)font{
    CGRect nameRect = [string boundingRectWithSize:CGSizeMake(0, rowHeight)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return nameRect;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"shouldChangeCharactersInRange = %@", textField.text);
    NSLog(@"replacementString = %ld", range.length);
    if (range.length == 1) {
        return YES;
    } else {
        NSString *textString = [textField.text stringByAppendingString:string];
        if ([textString floatValue] > [_returnMoney floatValue]) {
            [HMHUDManager showHUDWithText:@"应小于需退货商品最大额"];
            return NO;
        } else {
            if (range.length > 9) {
                return NO;
            } else {
                return YES;
            }
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (UITextField *)moneyLabel {
    if (_moneyLabel == nil) {
        _moneyLabel = [[UITextField alloc] init];
        self.moneyLabel.textColor = kff0000;
        
        
//        NSString *totalString = [NSString stringWithFormat:@"(最多¥%@)", _totalMoney];

        self.moneyLabel.placeholder = @"请输入退款金额";
        
        self.moneyLabel.font = [UIFont systemFontOfSize:15.f];
        self.moneyLabel.keyboardType = UIKeyboardTypeNumberPad;
        self.moneyLabel.delegate = self;
        [self addSubview:self.moneyLabel];
    }
    return _moneyLabel;
}

- (UILabel *)symbolLabel {
    if (_symbolLabel == nil) {
        _symbolLabel = [[UILabel alloc] init];
        _symbolLabel.text = @"¥";
        _symbolLabel.textColor = [UIColor redColor];
        _symbolLabel.frame = CGRectMake(CGRectGetMaxX(_nameLabel.frame), 0, 10, rowHeight);
        [self addSubview:_symbolLabel];
    }
    return _symbolLabel;
}


- (UILabel *)totalLabel {
    if (_totalLabel == nil) {
        _totalLabel = [[UILabel alloc] init];
        self.totalLabel.textColor = kc9c9c9;
        self.totalLabel.font = [UIFont systemFontOfSize:12.0];
        [self addSubview:self.totalLabel];
    }
    return _totalLabel;
}

@end
